const { db, query } = require("../db")

// 获取全部评论列表
exports.getallcommentlist = async (req, res, next) => {
  const { param } = req.query
  let list = await query(`select * from tb_comments;`)
  // console.log(list)
  let commentlist = []
  for (let i = 0; i < list.length; i++) {
    if (list[i].comment.indexOf(param) !== -1 || list[i].username.indexOf(param) !== -1) {
      commentlist.push(list[i])
    }
  }
  // console.log(commentlist)
  res.cc({ commentlist }, 200)
}

// 修改评论状态
exports.setcomment = async (req, res, next) => {
  const { id, isreport } = req.body
  let list = await query(`select * from tb_comments where id=${id};`)
  if (list.length !== 0 ) {
    await query(`update tb_comments set isreport=${isreport} where id=${id};`)
    res.cc({
      msg: '修改成功'
    }, 200)
  } else {
    res.cc({
      msg: '无此评论'
    })
  }
}

// 删除评论
exports.delcomment = async (req, res, next) => {
  const { id } = req.body
  let list = await query(`select * from tb_comments where id=${id};`)
  if (list.length !== 0 ) {
    await query(`delete from tb_comments where id=${id};`)
    res.cc({
      msg: '删除成功'
    }, 200)
  } else {
    res.cc({
      msg: '无此评论'
    })
  }
}